<?php

class wangzhanheadAction extends CommonAction
{
    public function index()
    {
        $model = new Model();
        $wherecondition = "t.projectid=p.id and p.employee=e.id and p.type=pt.id and  t.type=7 and t.site=s.id and t.status=0";
        $data = $model->table('ys_project p ,ys_task  t,ys_employee e,ys_projecttype pt,ys_site s')
            ->where($wherecondition)
            ->field('t.*,p.type,e.name as ename,pt.name as ptname,s.domain')
            ->order('t.id')
            ->select();
       // echo $model->getLastSql();exit();
        $this->list = $data;
        $this->display();
    }

    public function chuangjian()
    {
        $task=M("task");
        $taskid=I("get.id",0);
        if(empty($taskid)) $this->error("参数传递错误,任务编号为空");
        $taskinfo=$task->find($taskid);

        $tasktype = M('tasktype');
        $where['parentid'] = $taskinfo["type"];
        $where['id'] = array("exp", "not in ( SELECT type from ys_task where parentid=$taskid )");
        $this->list = $tasktype->where($where)->select();
        $this->defaulttime = date("Y-m-d H:i:s", strtotime("+5 days"));
        $this->assign('taskinfo', $taskinfo);

    /*
       $id = I('get.id');
        $this->taskid = $id;
     $model = new Model();
        $projectdata = $model->table('ys_site s ,ys_project p,ys_task t')
            ->where(" t.id=" . $id . " and p.id=s.projectid and t.site=s.id")
            ->field('s.*,p.content as pcon')
            ->find();

        $this->assign('chuangjian', $projectdata);*/

        $this->display();
    }

    public function addtask()
    {
        $task = D('task');
        $data = $task->create();
        if (!$data) {
            $this->error($data->getError());
        } else {
            $data["status"] = 0;
            if ($data['order'] > 1) $data["status"] = -1;
            $data['nextid'] = $data['order'] + 1;
            $data['preid'] = $data['order'] - 1;
            $data["createtime"] = date("Y-m-d H:i:s");
            $data["creator"] = I('session.userid', 0);
            $taskid = $task->add($data);

            $parentid = $data["parentid"];
            $ptaskinfo = $task->find($parentid);
            $plevel = $ptaskinfo["level"];
            $newdata["id"] = $taskid;
            $newdata["level"] = $plevel + 1; //更新级别

            $parentpath = $ptaskinfo["path"];
            if (empty($parentpath))
                $parentpath = "0,";
            $newdata["path"] = $parentpath . "," . $taskid;

            $task->save($newdata); //更新路径信息


            $this->assign("jumpUrl", U("wangzhanhead/woderenwu"));
            $this->success("创建成功");
        }
    }

    public function taskcontent()
    {
        $task = new taskModel();
        $data = $task->where("id='{$_GET['id']}'")->find();
        $tasktype=$this->CacheArray("tasktype");
        $sitedata=$this->CacheArray("site");
        $employee=$this->CacheArray("employee");

        $data["tasktype"]=$tasktype[$data["type"]]["name"];

        $siteinfo=$sitedata[$data["site"]];

        $data["sitedomain"]=$siteinfo["domain"];
        $data["sitename"]=$siteinfo["name"];

       /* dump($data);
        dump($employee);exit();*/
      //  foreach ($data as $k => &$v) {
        $data["creatername"]=$employee[$data["creator"]]["name"];
      //  }


        $this->assign("data", $data);
        $this->display();
    }

    public function back()
    {
        $id = I('get.id');
        $model = new Model();
        $where = "t.id=" . $id . " and t.projectid=p.id and t.site=s.id and p.type=pt.id";
        $data = $model->table('ys_project p ,ys_task  t,ys_projecttype pt,ys_site s')
            ->where($where)
            ->field('t.id as tid,t.name as tname,s.domain,pt.name as ptname')
            ->find();
        $this->assign('task', $data);
        $this->display();
    }

    public function yuanyin()
    {
        $task = M('task');
        $data = array();
        $data['id'] = I('post.taskid');
        $data['back'] = I('post.back');
        $data['status'] = -2;
        $count = $task->save($data);
        if ($count > 0) {
            $this->assign("waitSecond", 1);
            $this->assign("jumpUrl", U("wangzhanhead/woderenwu"));
            $this->success("已退回");
        } else {
            $this->error("没有退回");
        }
    }


    //完成之前上传任务结果
    public function wanchenginfo()
    {
        $task = M('task');
        $taskid = I('get.id');
        $data=$task->find($taskid);

        $tasktype=$this->CacheArray("tasktype");
        $sitedata=$this->CacheArray("site");


        $data["tasktype"]=$tasktype[$data["type"]]["name"];

        $siteinfo=$sitedata[$data["site"]];

        $data["sitedomain"]=$siteinfo["domain"];
        $data["sitename"]=$siteinfo["name"];
        $this->assign("data", $data);


        $this->display();
    }


    public function wancheng()
    {
        $task = M('task');
        $taskid = I('post.id');
        $data = array();
        $data['id'] = $taskid;
        $data['status'] = 2;
        $data['result'] =I('post.result','','htmlspecialchars');
        $data['finishtime'] =date("Y-m-d H:i:s");

       // dump($data);exit();

        $count = $task->save($data);

        $finishdata=$task->find($taskid);

        $nextorder=$finishdata["order"]+1;
     //  $othertask["order"];
        $othertask["status"]=0;

        $task->execute("update ys_task set status=0 where `order`='".$nextorder."'");

        /* echo $task->getLastSql();
        exit();*/

        if ($count > 0) {
            $this->assign("waitSecond", 1);
            $this->assign("jumpUrl", U("wangzhanhead/woderenwu"));
            $this->success("已完成");
        } else {
            $this->error("没有完成");
        }

    }

    public function gongzuoliang()
    {
        $this->display();
    }

    public function  renwulist()
    {
        $model = new Model();
        $name = I('post.sousuo');
        $where = "t.projectid=p.id and p.type=pt.id and t.site=s.id and t.status=2 and p.employee=e.id ";
        if (IS_POST || !empty($name)) {
            $where = $where . " and s.name like '%{$name}%'";
        }
        $data = $model->table('ys_project p ,ys_task t,ys_projecttype pt,ys_employee e,ys_site s')
            ->where($where)
            ->field('t.name as tname,pt.name as ptname,s.*,e.name as ename,t.createtime as ct,t.deadline as dt')
            ->order('t.id')
            ->select();

        //echo $model->getLastSql();exit();
        $this->list = $data;
        $this->display();
    }

    public function taketask()
    {
        $task = M('task');
        $taskid = I('post.taskid');
        if(empty($taskid)) $this->error("参数传递错误,任务编号没有");
        $data = array();
        $data['id'] = $taskid;
        $data['taker'] = I('session.userid', 0);
        $data['begintime'] = date("Y-m-d H:i:s");
        $data['status'] = "1";
        $task->save($data);
        $this->assign("waitSecond",4);
        $this->assign("jumpUrl",U("woderenwu" ));
        $this->success("任务领取成功");
    }
    public function woderenwu()
    {
        $site = M('task');
        $newtypdata = $this->CacheArray("tasktype");

        $name = I('post.sousuo');
        $where['parentid'] = 0;
        $where['taker'] = I('session.userid', 0);
      //  $where['status'] = array('eq', 1);;
        if (IS_POST || !empty($name)) {
            $where['name'] = array('like', "%{$name}%");
        }
        /*     $this->list = $site->where($where)->select();*/
        $data = $site->where($where)->order('id desc')->select();
        $task = M("task");
        foreach ($data as $k1 => &$v1) {
            $taskcon["parentid"] = $v1["id"];
            //$taskcon['taker'] = I('session.userid', 0);
            $taskdata = $task->where($taskcon)->select();
            foreach ($taskdata as $k => &$v) {
                $v["type"] = $newtypdata[$v["type"]]["name"];
            }
            $v1["task"] = $taskdata;
        }
        $this->list = $data;
        $this->display();
    }

    public function taskdel()
    {
        $task = M('task');
        $id = $_GET['id'];
        $res = $task->delete($id);
        if ($res > 0) {
            $this->success("删除成功");
        } else {
            $this->error("删除失败");
        }
    }

    public function pingfen()
    {
      //  dump($_POST);exit;
        $task = M('task');
        $data = array();
        $data['id'] = I('post.fenid');
        $data['score'] = I('post.fen');
        if ($data['score'] > 100) {
            $data['score'] = 100;
        } else if ($data['score'] < 0) {
            $data['score'] = 0;
        }
        $task->save($data);
        $this->redirect('wangzhanhead/woderenwu');

    }

}